﻿using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using SGLLv2.UI.Properties;

namespace SGLLv2.UI
{
    public partial class FormPrintPreview : Template.FormTemplate, IMethods
    {
        // Declare Global Variables
        private MDIParent mdiParent;
        private readonly ReportDataSource reportDataSource;

        // Declare enumerations
        public enum ReportTypes
        {
            Calls,
            Complaints,
            Services
        }

        // Declare Public Variables
        private DateTime _startDate;
        private DateTime _endDate;
        private readonly ReportTypes _reportType;
        private readonly string _depositReceipt;
        private readonly string _proofOfPayment;
        private readonly string _processOrder;
        private readonly string _messengerID;
        private readonly int _serviceID;
        private readonly string _comments;
        private readonly Form _callerForm;

        // Class Constructor
        public FormPrintPreview(Form callerForm, IEnumerable dataTable, DateTime startDate, DateTime endDate, ReportTypes reportType)
        {
            InitializeComponent();

            // Initialize variables
            _callerForm = callerForm;
            reportDataSource = new ReportDataSource();
            _startDate = startDate;
            _endDate = endDate;
            reportDataSource.Name = Settings.Default.DataSetName;

            // Set data to the Binding Source
            if (reportType.Equals(ReportTypes.Calls))
            {
                SP_CALLS_SELECTBindingSource.DataSource = dataTable;
                reportDataSource.Value = SP_CALLS_SELECTBindingSource;

                reportViewer.LocalReport.DataSources.Add(reportDataSource);
                reportViewer.LocalReport.ReportEmbeddedResource = Settings.Default.CallsReport;
            }
            else
            {
                SP_COMPLAINTS_SELECTBindingSource.DataSource = dataTable;
                reportDataSource.Value = SP_COMPLAINTS_SELECTBindingSource;

                reportViewer.LocalReport.DataSources.Add(reportDataSource);
                reportViewer.LocalReport.ReportEmbeddedResource = Settings.Default.ComplaintsReport;
            }
        }
        public FormPrintPreview(Form callerForm, int serviceID, DateTime startDate, DateTime endDate, ReportTypes reportType, string depositReceipt, string proofOfPayment, string processOrder, string messengerID, string comments)
        {
            InitializeComponent();

            // Initialize variables
            _callerForm = callerForm;
            reportDataSource = new ReportDataSource();
            _startDate = startDate;
            _endDate = endDate;
            _reportType = reportType;
            _depositReceipt = depositReceipt;
            _proofOfPayment = proofOfPayment;
            _processOrder = processOrder;
            _messengerID = messengerID;
            _serviceID = serviceID;
            _comments = comments;
            reportDataSource.Name = Settings.Default.DataSetName;

            SP_SERVICES_DETAILS_REPORTTableAdapter.Fill(DataSetSGLL.SP_SERVICES_DETAILS_REPORT, serviceID);
            reportDataSource.Value = SP_SERVICES_DETAILS_REPORTBindingSource;

            reportViewer.LocalReport.DataSources.Add(reportDataSource);
            reportViewer.LocalReport.ReportEmbeddedResource = Settings.Default.ServicesReport;



        }
        private void FormPrintPreview_Load(object sender, EventArgs e)
        {
            // Declare Local Variables
            mdiParent = (MDIParent)(ParentForm);
            if (mdiParent == null) return;

            // Declaring and initializing the parameters.
            ReportParameter reportParameterStartDate = new ReportParameter("ReportParameterStartDate", _startDate.ToString(_reportType.Equals(ReportTypes.Services) ? Settings.Default.CustomFormatDateTime : Settings.Default.CustomFormatDate), false);
            ReportParameter reportParameterEndDate = new ReportParameter("ReportParameterEndDate", _endDate.ToString(_reportType.Equals(ReportTypes.Services) ? Settings.Default.CustomFormatDateTime : Settings.Default.CustomFormatDate), false);
            ReportParameter reportParameterWorkstation = new ReportParameter("ReportParameterWorkstation", Environment.MachineName, false);
            ReportParameter reportParameterUsername = new ReportParameter("ReportParameterUsername", Environment.UserName, false);

            if (_reportType.Equals(ReportTypes.Services))
            {
                ReportParameter reportParameterDepositReceipt = new ReportParameter("ReportParameterDepositReceipt", _depositReceipt, false);
                ReportParameter reportParameterProofOfPayment = new ReportParameter("ReportParameterProofOfPayment", _proofOfPayment, false);
                ReportParameter reportParameterProcessOrder = new ReportParameter("ReportParameterProcessOrder", _processOrder, false);
                ReportParameter reportParameterMessengerID = new ReportParameter("ReportParameterMessengerID", _messengerID, false);
                ReportParameter reportParameterServiceID = new ReportParameter("ReportParameterServiceID", _serviceID.ToString(CultureInfo.InvariantCulture), false);
                ReportParameter reportParameterComments = new ReportParameter("ReportParameterComments",
                                                                              string.IsNullOrWhiteSpace(_comments) ? Resources.MessageEmptyComments : _comments, false);

                reportViewer.LocalReport.SetParameters(reportParameterDepositReceipt);
                reportViewer.LocalReport.SetParameters(reportParameterProofOfPayment);
                reportViewer.LocalReport.SetParameters(reportParameterProcessOrder);
                reportViewer.LocalReport.SetParameters(reportParameterMessengerID);
                reportViewer.LocalReport.SetParameters(reportParameterServiceID);
                reportViewer.LocalReport.SetParameters(reportParameterComments);
            }

            // Load the parameters into the report.
            reportViewer.LocalReport.SetParameters(reportParameterStartDate);
            reportViewer.LocalReport.SetParameters(reportParameterEndDate);
            reportViewer.LocalReport.SetParameters(reportParameterWorkstation);
            reportViewer.LocalReport.SetParameters(reportParameterUsername);

            reportViewer.ZoomMode = ZoomMode.PageWidth;

            var rst = reportViewer.LocalReport.GetDefaultPageSettings();
            if (reportViewer.ParentForm != null && reportViewer.ParentForm.Width > rst.PaperSize.Width)
            {
                var padding = (reportViewer.ParentForm.Width - rst.PaperSize.Height) / 2;
                reportViewer.Padding = new Padding(padding - 20, 1, padding - 20, 1);
            }

            reportViewer.LocalReport.Refresh();
            reportViewer.RefreshReport();

            //if (reportViewer.LocalReport.GetTotalPages() != 1)
            //   reportViewer.CurrentPage = reportViewer.CurrentPage + 1;

            //reportViewer.LocalReport.Refresh();
            //reportViewer.RefreshReport();
        }
        private void FormPrintPreview_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Declare Local Variables
            IMethods methods = (IMethods)_callerForm;

            // Validations
            if (methods != null) methods.EnablePrintButton();
        }

        // Interface
        public void New()
        {
        }
        public void SetControlBox()
        {
            mdiParent.toolStripMenuItemClear.Enabled = mdiParent.toolStripButtonClear.Enabled = true;

            //mdiParent.toolStripMenu.BindingSource.
        }
        public void ClearFilter()
        {
        }
        public void ClearForm()
        {
            //mdiParent.
        }
        public void Save()
        {
        }
        public void Print()
        {
        }
        public void ChangeFormView(Global.FormViewModes mode)
        {
        }
        public void EnablePrintButton()
        {

        }
        public void Export()
        {
            // Declare Local Variables
            Warning[] warnings;
            string[] streamIDs;
            string mimeTypes, enconding, extension;
            const string deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat><PageWidth>11in</PageWidth><PageHeight>8.5in</PageHeight><MarginTop>0.25in</MarginTop><MarginLeft>0.25in</MarginLeft><MarginRight>0.25in</MarginRight><MarginBottom>0.25in</MarginBottom></DeviceInfo>";

            var bytes = reportViewer.LocalReport.Render("PDF", deviceInfo, out mimeTypes, out enconding, out extension, out streamIDs, out warnings);
            using (FileStream file = new FileStream(@"c:\pdf.pdf", FileMode.CreateNew, FileAccess.Write))
            {
                file.Write(bytes, 0, bytes.Length);
            }
        }
    }
}